﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LP580.CG
{
    /// <summary>
    /// clsFavoriteCompany 的摘要说明
    /// <summary>
    public class clsFavoriteCompany
    {
        #region 构造函数

        public clsFavoriteCompany() { }

        #endregion


        #region 增删改数据库操作

        public static int Insert(clsFavoriteCompany prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = string.Format("insert into [tbl_FavoriteCompany](" +
                            " [UserID]," +
                            " [CompanyID]" +
            ") values (" +
                    "@UserID," +
                     "@CompanyID" +
            ")");
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@CompanyID", prmEntity._companyid);
            SaveDTM.Insert();

            //对应店铺收藏数增一
            clsUserCompanyInfo.AddFavorite(prmEntity._companyid);

            return 1;
        }

        public static bool Delete(int prmUserID, int prmCompanyID)
        {
            DBase dbo = new DBase();
            dbo.ExecSql("DELETE FROM [tbl_FavoriteCompany] WHERE UserID=" + prmUserID.ToString() + " and CompanyID=" + prmCompanyID.ToString());
            dbo.CloseConn();
            dbo = null;

            //对应店铺收藏数减一
            clsUserCompanyInfo.DecFavorite(prmCompanyID);

            return true;
        }

        #endregion

        /// <summary>
        /// 获取一个用户收藏了哪些店铺
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static List<clsUserCompanyInfo> GetFavoriteCompanyCollection(int userid)
        {
            List<clsUserCompanyInfo> ls = new List<clsUserCompanyInfo>();
            DBase dbo = new DBase();
            string sql = "SELECT CompanyID FROM [tbl_FavoriteCompany] WHERE UserID=" + userid + " ORDER BY CompanyID DESC";
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(new clsUserCompanyInfo((int)dr[0]));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }


        /// <summary>
        /// 获取一家店铺被哪些用户收藏
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static List<clsUserCompanyInfo> GetFavoriteUserCollection(int companyID)
        {
            List<clsUserCompanyInfo> ls = new List<clsUserCompanyInfo>();
            DBase dbo = new DBase();
            string sql = "SELECT UserID FROM [tbl_FavoriteCompany] WHERE CompanyID=" + companyID + " ORDER BY UserID DESC";
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(new clsUserCompanyInfo((int)dr[0]));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        #region 字段属性

        private int _userid = 0;
        /// <summary>
        /// 所属用户
        /// </summary>
        public int UserID
        {
            get { return _userid; }
            set { _userid = value; }
        }

        private int _companyid = 0;
        /// <summary>
        /// 被收藏公司ID也即用户ID
        /// </summary>
        public int CompanyID
        {
            get { return _companyid; }
            set { _companyid = value; }
        }

        #endregion
    }
}
